﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace TextEditorDP
{
    #region WordPad Menu Class
    public class WordPadMenu: Widget
    {
        #region	Local Variables
        MainMenu menu;
        MenuItem file, edit, insert;
        MenuItem mnew, open, save, saveas, exit;
        MenuItem cut, copy, paste, clear, selectall, find, findnext, replace;
        MenuItem datetime, rtf, picture;
        StatusBar status;
        WordPad wordpad;

        public MainMenu MainMenu
        {
            get { return menu; }
        }
        #endregion	// Local Variables

        #region Constructor
        public WordPadMenu(WordPad wordpad, CStatusBar status)
        {
            this.status = status;
            this.wordpad = wordpad;

            // FILE menu
            file = new MenuItem("&File");
            mnew = new MenuItem(MenuMerge.Add, 0, Shortcut.CtrlN, "&New...", new EventHandler(NewDocument), null, new EventHandler(MenuClickHandler), null);
            open = new MenuItem(MenuMerge.Add, 0, Shortcut.CtrlO, "&Open...", new EventHandler(OpenDocument), null, new EventHandler(MenuClickHandler), null);
            save = new MenuItem(MenuMerge.Add, 0, Shortcut.CtrlS, "&Save...", new EventHandler(SaveDocument), null, new EventHandler(MenuClickHandler), null);
            saveas = new MenuItem(MenuMerge.Add, 0, Shortcut.None, "Save &As...", new EventHandler(SaveAs), null, new EventHandler(MenuClickHandler), null);
            exit = new MenuItem(MenuMerge.Add, 0, Shortcut.None, "E&xit", new EventHandler(Exit), null, new EventHandler(MenuClickHandler), null);
            file.MenuItems.AddRange(new MenuItem[] { mnew, open, save, saveas, exit });


            // EDIT menu
            edit = new MenuItem("&Edit");
            cut = new MenuItem(MenuMerge.Add, 0, Shortcut.CtrlX, "Cu&t", new EventHandler(DoCut), null, new EventHandler(MenuClickHandler), null);
            copy = new MenuItem(MenuMerge.Add, 0, Shortcut.CtrlC, "&Copy", new EventHandler(DoCopy), null, new EventHandler(MenuClickHandler), null);
            paste = new MenuItem(MenuMerge.Add, 0, Shortcut.CtrlV, "&Paste", new EventHandler(DoPaste), null, new EventHandler(MenuClickHandler), null);
            clear = new MenuItem(MenuMerge.Add, 0, Shortcut.Del, "Cle&ar", new EventHandler(DoClear), null, new EventHandler(MenuClickHandler), null);
            selectall = new MenuItem(MenuMerge.Add, 0, Shortcut.CtrlA, "Select A&ll", new EventHandler(DoSelectAll), null, new EventHandler(MenuClickHandler), null);
            find = new MenuItem(MenuMerge.Add, 0, Shortcut.CtrlF, "&Find...", new EventHandler(DoFind), null, new EventHandler(MenuClickHandler), null);
            findnext = new MenuItem(MenuMerge.Add, 0, Shortcut.F3, "Find &Next", new EventHandler(DoFindNext), null, new EventHandler(MenuClickHandler), null);
            replace = new MenuItem(MenuMerge.Add, 0, Shortcut.CtrlH, "R&eplace...", new EventHandler(DoReplace), null, new EventHandler(MenuClickHandler), null);
            edit.MenuItems.AddRange(new MenuItem[] { cut, copy, paste, clear, selectall, find, findnext, replace });

            // INSERT menu
            insert = new MenuItem("&Insert");
            datetime = new MenuItem(MenuMerge.Add, 0, Shortcut.None, "&Date and Time...", new EventHandler(InsertDateTime), null, new EventHandler(MenuClickHandler), null);
            rtf = new MenuItem(MenuMerge.Add, 0, Shortcut.None, "&RTF", new EventHandler(InsertRTF), null, new EventHandler(MenuClickHandler), null);
            picture = new MenuItem(MenuMerge.Add, 0, Shortcut.None, "&Picture", new EventHandler(InsertPicture), null, new EventHandler(MenuClickHandler), null);
            insert.MenuItems.AddRange(new MenuItem[] { datetime, rtf, picture });

            menu = new MainMenu(new MenuItem[] { file, edit, insert });
        }
        #endregion	// Constructor

        #region Event Handlers
        private void MenuClickHandler(object sender, EventArgs e)
        {
            if (status != null)
            {
                if (sender == mnew) status.Text = "Create new document";
                if (sender == open) status.Text = "Open existing document";
                if (sender == save) status.Text = "Save current document";
                if (sender == saveas) status.Text = "Save current document under new name";
                if (sender == exit) status.Text = "Exit application";

                if (sender == cut) status.Text = "Cut the selection and put it into the clipboard";
                if (sender == copy) status.Text = "Copy the selection into the clipboard";
                if (sender == paste) status.Text = "Paste clipboard contents";
                if (sender == clear) status.Text = "Remove selected text";
                if (sender == selectall) status.Text = "Select the whole document";
                if (sender == find) status.Text = "Find text";
                if (sender == findnext) status.Text = "Repeat last find operation";
                if (sender == replace) status.Text = "Find and replace text";

                if (sender == datetime) status.Text = "Insert current date and time";
                if (sender == rtf) status.Text = "Insert rtf";
                if (sender == picture) status.Text = "Insert picture";
            }
        }

        // FILE
        private void NewDocument(object sender, EventArgs e)
        {
            wordpad.Clear();
        }

        private void OpenDocument(object sender, EventArgs e)
        {
            wordpad.Open();
        }

        private void SaveDocument(object sender, EventArgs e)
        {
            wordpad.Save(false);
        }

        private void SaveAs(object sender, EventArgs e)
        {
            wordpad.Save(true);
        }

        private void Exit(object sender, EventArgs e)
        {
            wordpad.Exit();
        }

        private void DoCut(object sender, EventArgs e)
        {
            wordpad.DoCut();
        }

        private void DoCopy(object sender, EventArgs e)
        {
            wordpad.DoCopy();
        }

        private void DoPaste(object sender, EventArgs e)
        {
            wordpad.DoPaste();
        }

        private void DoClear(object sender, EventArgs e)
        {
            wordpad.DoClear();

        }

        private void DoSelectAll(object sender, EventArgs e)
        {
            wordpad.DoSelectAll();
        }

        private void DoFind(object sender, EventArgs e)
        {
            wordpad.DoFind();
        }

        private void DoFindNext(object sender, EventArgs e)
        {
            wordpad.DoFindNext();
        }

        private void DoReplace(object sender, EventArgs e)
        {
            wordpad.DoReplace();
        }

        // INSERT
        private void InsertDateTime(object sender, EventArgs e)
        {
            wordpad.InsertDateTime();
        }

        private void InsertRTF(object sender, EventArgs e)
        {
            wordpad.InsertRTF();
        }

        private void InsertPicture(object sender, EventArgs e)
        {
            wordpad.InsertPicture();
        }

        #endregion

        #region Implement Interface
        protected HelpHandler _successor;
        protected string _topic;
        public virtual bool HasHelp()
        {
            if (_topic == "")
                return false;
            return true;
        }

        public virtual void SetHandler(HelpHandler successor, string topic)
        {
            _successor = successor;
            _topic = topic;
        }

        public virtual void HandleHelp()
        {
            if (HasHelp())
            {
                MessageBox.Show(_topic);
            }
            else
            {
                if (_successor != null)
                {
                    _successor.HandleHelp();
                }
            }
        }
        #endregion
    }
    #endregion	// WordPad Menu Class
}
